package  com.wmm.job.client;

import com.wmm.job.model.User;
import com.wmm.job.model.UserMessage;
import com.wmm.job.student.*;
import com.wmm.job.test.ManageTest;
import com.wmm.job.test.TestItem;

import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.List;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    /**
     * 接收到的集合
     */
    private static HashMap<String, Object> fromData;
    /**
     * 发送数据的集合
     */
    private static HashMap<String, Object> toData;

    /**
     * 通过构造方法实现成员变量的初始化
     *
     * @param cic
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            //Scanner sc = new Scanner(System.in);
            //int choose = sc.nextInt();
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统...");
                    clientStudentLogin();
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();

        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        toMessage(2, tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        fromMessage();

        tum = (UserMessage) fromData.get("data");
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            //界面展示
            clientSencondPage();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    /**
     *
     */
    private void clientStudentLogin() throws IOException, ClassNotFoundException {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入密码信息：");
        String password = ClientScanner.getScanner().next();

        UserMessage tum = new UserMessage("studentCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        toMessage(1, tum);
        System.out.println("客户端发送学生账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        fromMessage();
        if ("1".equals(fromData.get("type").toString())) {
            System.out.println("登录成功，欢迎使用！");
            showStudentMenu(tum.getUser().getUserName());
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    /**
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void clientSencondPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员管理");
            System.out.println("     [2] 试卷管理");
            System.out.println("   [0] 返回");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");

            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    //学生管理
                    System.out.println("正在进入学员管理系统...");
                    showMenu("1");
                    break;
                case 2:
                    //试卷管理
                    System.out.println("正在进入试卷管理系统...");
                    showMenu("2");
                    break;
                case 0:
                    System.out.println("正在返回...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }


    public void showMenu(String type) {
        if (type.equals("1")) {
            while (true) {
                System.out.println("\n\n\t       学生信息管理系统");
                System.out.println("-------------------------------------------");
                System.out.print("   [1] 增加学生信息");
                System.out.println("         [2] 删除学生信息");
                System.out.print("   [3] 修改学生信息");
                System.out.println("         [4] 查找学生信息");
                System.out.println("         [0] 退出学生系统");
                System.out.println("-------------------------------------------");
                System.out.println("请选择具体的业务编号:");
                int choose = ClientScanner.getScanner().nextInt();
                switch (choose) {
                    case 1:
                        addStudent();
                        break;
                    case 2:
                        deleteStudent();
                        break;
                    case 3:
                        modifyStudent();
                        break;
                    case 4:
                        queryStudent();
                        break;
                    case 0:
                        System.out.print("谢谢使用，再见！");
                        return;
                    default:
                        System.out.println("输入错误，请重新输入");
                }

            }
        } else if (type.equals("2")) {
            while (true) {
                System.out.println("\n\n\t       考题信息管理系统");
                System.out.println("-------------------------------------------");
                System.out.print("   [1] 增加考题");
                System.out.println("         [2] 删除考题");
                System.out.print("   [3] 修改考题");
                System.out.println("         [4] 查询考题");
                System.out.println("         [0] 退出考题系统");
                System.out.println("-------------------------------------------");
                System.out.println("请选择具体的业务编号:");
                int choose = ClientScanner.getScanner().nextInt();
                switch (choose) {
                    case 1:
                        addTest();
                        break;
                    case 2:
                        deleteTest();
                        break;
                    case 3:
                        modifyTest();
                        break;
                    case 4:
                        queryTest();
                        break;
                    case 0:
                        System.out.print("谢谢使用，再见！");
                        return;
                    default:
                        System.out.println("输入错误，请重新输入");
                }

            }
        }
    }

    public void showStudentMenu(String id){
        while (true) {
            System.out.println("\n\n\t       考题信息管理系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 修改信息");
            System.out.println("         [2] 开始考试");
            System.out.print("   [3] 查询考试成绩");
            System.out.println("         [4] 修改密码");
            System.out.println("         [0] 退出学生系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择具体的业务编号:");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    modifyStudent(id);
                    break;
                case 2:
                    startTest(id);
                    break;
                case 3:
                    queryScore(id);
                    break;
                case 4:
                    modifyPassWord(id);
                    break;
                case 0:
                    System.out.print("谢谢使用，再见！");
                    return;
                default:
                    System.out.println("输入错误，请重新输入");
            }

        }
    }

    private void addStudent() {
        System.out.println("请输入要增加的学生信息(学号,姓名,年龄)：");
        String strStudent = ClientScanner.getScanner().next();

        // 2.按照逗号进行字符串拆分，使用字符串数组进行记录
        String[] strings = strStudent.split(",");
        try {
            Student st = new Student(Integer.parseInt(strings[0]), strings[1], Integer.parseInt(strings[2]), "123456");
            toMessage(211, st);
            System.out.println("客户端发送信息成功！");
            if ((Integer) fromData.get("type") == 1) {
                System.out.println("学生信息添加成功");
            } else {
                System.out.println("学生信息添加失败");
            }
        } catch (IdException e) {
            e.printStackTrace();
        } catch (AgeException e) {
            e.printStackTrace();
        }
    }

    private void deleteStudent() {
        System.out.println("请输入要删除的学生学号：");
        int id = ClientScanner.getScanner().nextInt();
        toMessage(212, id);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            System.out.println("删除学生成功");
        } else {
            System.out.println("删除学生失败");
        }
    }

    private void modifyStudent() {
        System.out.println("请输入要修改的学生信息(学号,姓名,年龄)：");
        String strStudent = ClientScanner.getScanner().next();
        // 2.按照逗号进行字符串拆分，使用字符串数组进行记录
        String[] strings = strStudent.split(",");
        Student st = null;
        try {
            st = new Student(Integer.parseInt(strings[0]), strings[1], Integer.parseInt(strings[2]));
            toMessage(213, st);
            System.out.println("客户端发送信息成功！");
            fromMessage();
            if ((Integer) fromData.get("type") == 1) {
                System.out.println("学生信息修改成功");
            } else {
                System.out.println("学生信息修改失败");
            }
        } catch (IdException e) {
            e.printStackTrace();
        } catch (AgeException e) {
            e.printStackTrace();
        }
    }

    private void queryStudent() {
        System.out.println("请输入要查询的学生学号：");
        int id = ClientScanner.getScanner().nextInt();
        toMessage(214, id);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            Student st = (Student) fromData.get("data");
            System.out.println("查找成功，查找到的学生信息是：" + st);
        } else {
            System.out.println("学生信息查询失败");
        }
    }

    private void modifyStudent(String id) {
        System.out.println("请输入要修改的学生信息(姓名,年龄)：");
        String strStudent = ClientScanner.getScanner().next();
        // 2.按照逗号进行字符串拆分，使用字符串数组进行记录
        String[] strings = strStudent.split(",");
        Student st = null;
        try {
            st = new Student(Integer.parseInt(id), strings[0], Integer.parseInt(strings[1]));
            toMessage(11, st);
            System.out.println("客户端发送信息成功！");
            fromMessage();
            if ((Integer) fromData.get("type") == 1) {
                System.out.println("学生信息修改成功");
            } else {
                System.out.println("学生信息修改失败");
            }
        } catch (IdException e) {
            e.printStackTrace();
        } catch (AgeException e) {
            e.printStackTrace();
        }
    }

    private void addTest() {
        // 1.根据提示输入信息
        System.out.println("请输入要增加的试题编号：");
        int testid = ClientScanner.getScanner().nextInt();
        System.out.println("请输入要增加的试题题目：");
        String test = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择A：");
        String choseA = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择B：");
        String choseB = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择C：");
        String choseC = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择D：");
        String choseD = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题答案：");
        String answer = ClientScanner.getScanner().next();

        TestItem item = new TestItem(testid, test, choseA, choseB, choseC, choseD, answer);
        toMessage(221, item);
        System.out.println("客户端发送信息成功！");
        if ((Integer) fromData.get("type") == 1) {
            System.out.println("试题信息添加成功");
        } else {
            System.out.println("试题信息添加失败");
        }

    }

    private void deleteTest() {
        System.out.println("请输入要删除的试题编号：");
        int id = ClientScanner.getScanner().nextInt();
        toMessage(222, id);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            System.out.println("删除试题成功");
        } else {
            System.out.println("删除试题失败");
        }
    }

    private void modifyTest() {
        // 1.根据提示输入信息
        System.out.println("请输入要增加的试题编号：");
        int testid = ClientScanner.getScanner().nextInt();
        System.out.println("请输入要增加的试题题目：");
        String test = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择A：");
        String choseA = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择B：");
        String choseB = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择C：");
        String choseC = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题选择D：");
        String choseD = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的试题答案：");
        String answer = ClientScanner.getScanner().next();
        String strStudent = ClientScanner.getScanner().next();
        // 2.按照逗号进行字符串拆分，使用字符串数组进行记录
        TestItem item = new TestItem(testid, test, choseA, choseB, choseC, choseD, answer);
        toMessage(223, item);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            System.out.println("试题信息修改成功");
        } else {
            System.out.println("试题信息修改失败");
        }
    }

    private void queryTest() {
        System.out.println("请输入要查询的试题编号：");
        int id = ClientScanner.getScanner().nextInt();
        toMessage(224, id);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            TestItem item = (TestItem) fromData.get("data");
            System.out.println("查找成功，查找到的试卷信息是：" + item);
        } else {
            System.out.println("试题信息查询失败");
        }
    }

    private void startTest(String id){
        toMessage(12,null);
        System.out.println("客户端发送信息成功！");
        fromMessage();

        List<TestItem> list = (List<TestItem>)fromData.get("data");
        System.out.println("考试开始：");

        // 显示服务器发送来的数据
        int num = list.size();
        // 提示用户多少道题
        System.out.println("一共有" + num + "道题。");
        int score = 0;
        int count = 1;
        // 使用foreach循环来遍历集合
        for (TestItem ti1 : list) {
            //用来提示第几题
            System.out.println("*****第"+count+"题*****");
            System.out.println(ti1.getTest());
            System.out.println("A:" + ti1.getChoseA());
            System.out.println("B:" + ti1.getChoseB());
            System.out.println("C:" + ti1.getChoseC());
            System.out.println("D:" + ti1.getChoseD());
            System.out.println("*****\t请输入答案：\t*****");
            String chose = ClientScanner.getScanner().next();
            if (ti1.getAnswer().equals(chose)) {
                score += 100/num;
            }
            count++;
        }
        System.out.println("*****分数是" + score + "*****");
        // 把得分和服务器功能序号发送到服务器
        System.out.println("*****考试结束*****");
        //考试结束，发送消息和分数到服务器在case内发送消息。
        try {
            Student st = new Student(Integer.parseInt(id),score);
            toMessage(15,st);
            //加一句接收消息的语句
            fromMessage();
        } catch (IdException e) {
            e.printStackTrace();
        }
    }

    private void queryScore(String id){
        toMessage(13,id);
        System.out.println("客户端发送信息成功！");
        fromMessage();
        if ((Integer) fromData.get("type") == 1) {
            String score = fromData.get("data").toString();
            System.out.println("查找成功，得分:" +score);
        } else {
            System.out.println("没有查询到分数");
        }
    }

    private void modifyPassWord(String id){
        String password = "";
        try {
            System.out.println("请输入新的密码：");
            password = ClientScanner.getScanner().next();
            Student st = new Student(Integer.parseInt(id),password);
            toMessage(14,st);
            System.out.println("客户端发送信息成功！");
            fromMessage();
        } catch (IdException e) {
            e.printStackTrace();
        }
    }

    //向服务器发送消息
    private void toMessage(int type, Object data) {
        toData = new HashMap<>();
        toData.put("type", type);
        toData.put("data", data);
        //System.out.println(toData.get("type") + "发送消息");
        try {
            cic.getOos().writeObject(toData);
            cic.getOos().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //从服务器接收消息
    private void fromMessage() {
        try {
            fromData = (HashMap<String, Object>) cic.getOis().readObject();
            //System.out.println("客户端收到" + fromData.get("type"));
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

